<?php
namespace Order\Controller;


class IndexController extends \Admin\Common\CommonController
{
   /**
    * 订单列表
    */
   public function lists(){
    $orders=$this->orders();
       $this->assign('orders',$orders);
//       dd($orders);
     $this->display();
   }

    /**
     * @return mixed
     * 查看订单的详情
     */
    public  function  orderinfo(){
        $uoid=I('get.uoid');
        $uid=I('get.uid');
//        dd($uoid);die;
         //查出该订单的所有内容
        $good=M('userorder')->join('sp_payorder ON sp_userorder.uoid=sp_payorder.uoid')
            ->join("sp_user ON sp_userorder.uid=sp_user.uid")
            ->where("sp_userorder.uid=$uid AND sp_userorder.uoid=$uoid")->find();
        $good['orderinfo']=json_decode($good['orderinfo'],true);
        $this->assign('good',$good);
//        dd($good);
        $this->display();

    }

    /**
     * @return mixed
     * 发货
     */
    public  function shipping(){
       $uoid=I('get.uoid');
        //更改订单表中的状态
        $res=M('userorder')->where("uoid=$uoid")->setField('orderstatus',1);
//        查找订单中的每样商品的ID
        $good=M('userorder')->join('sp_payorder ON sp_userorder.uoid=sp_payorder.uoid')->where("sp_userorder.uoid=$uoid")->find();
        $goods=json_decode($good['orderinfo'],true);
        //获得只包含ugid 的数组
        $ugid=[];
        foreach($goods as $k=>$v){
             $ugid[]=$v['ugid'];
        }
        //将usergoods的商品状态改为2，表示已发货
        $map['ugid']=['in',$ugid];
        $res=M('usergoods')->where($map)->setField('gstatus',2);
        $this->success('发货成功',U('lists'));die;
    }
    private function  orders(){
        return M('userorder')->select();
    }
}